package com.uberspot.yangchenglun

import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var editTextNumber1: EditText
    private lateinit var editTextNumber2: EditText
    private lateinit var spinnerOperations: Spinner
    private lateinit var textViewResult: TextView
    private lateinit var buttonCalculate: Button
    private lateinit var buttonHistory: Button
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setTitle("杨程仑2022110354") // 设置标题

        editTextNumber1 = findViewById(R.id.editTextNumber1)
        editTextNumber2 = findViewById(R.id.editTextNumber2)
        spinnerOperations = findViewById(R.id.spinnerOperations)
        textViewResult = findViewById(R.id.textViewResult)
        buttonCalculate = findViewById(R.id.buttonCalculate)
        buttonHistory = findViewById(R.id.buttonHistory)
        sharedPreferences = getSharedPreferences("AppPreferences", MODE_PRIVATE)

        val adapter = ArrayAdapter.createFromResource(
            this,
            R.array.operations_array,
            android.R.layout.simple_spinner_item
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinnerOperations.adapter = adapter

        buttonCalculate.setOnClickListener {
            calculate()
        }

        buttonHistory.setOnClickListener {
            startActivity(Intent(this, HistoryActivity::class.java))
        }
    }

    private fun calculate() {
        // 获取输入的数字
        val num1 = editTextNumber1.text.toString().toDoubleOrNull()
        val num2 = editTextNumber2.text.toString().toDoubleOrNull()

        // 确保两个数字都已输入且有效
        if (num1 == null || num2 == null) {
            textViewResult.text = "Error: Please enter valid numbers."
            return
        }

        // 获取选择的操作
        val operation = spinnerOperations.selectedItem.toString()

        // 根据选择的操作执行计算
        val result = when (operation) {
            "Addition" -> num1 + num2
            "Subtraction" -> num1 - num2
            "Multiplication" -> num1 * num2
            "Division" -> {
                // 确保除数不为零
                if (num2 == 0.0) {
                    textViewResult.text = "Error: Division by zero."
                    return
                } else {
                    num1 / num2
                }
            }
            else -> {
                textViewResult.text = "Error: Unknown operation."
                return
            }
        }
        // 显示结果
        textViewResult.text = result.toString()

        // 保存历史记录
        saveHistory("$num1 $operation $num2 = $result")
    }

    private fun saveHistory(history: String) {
        val historyList = loadHistory().toMutableSet() // 将 Set 转换为 MutableSet
        historyList.add(history)
        val editor = sharedPreferences.edit()
        editor.putStringSet("history", historyList)
        editor.apply()
    }

    private fun loadHistory(): Set<String> {
        return sharedPreferences.getStringSet("history", null) ?: setOf()
    }
}